home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / CalculateFrame.java < prev    next >
Text File  |  1998-11-02  |  8KB  |  250 lines

  1. /*
  2.     A basic extension of the java.awt.Frame class
  3.  */
  4.  
  5. import java.awt.*;
  6.  
  7. import symantec.itools.awt.KeyPressManagerPanel;
  8. public class CalculateFrame extends Frame {
  9.  
  10.     void Frame1_WindowClosing(java.awt.event.WindowEvent event)
  11.     {
  12.         dispose();         // hide the Frame
  13.     }
  14.  
  15.     public CalculateFrame()
  16.     {
  17.         // This code is automatically generated by Visual Cafe when you add
  18.         // components to the visual environment. It instantiates and initializes
  19.         // the components. To modify the code, only use code syntax that matches
  20.         // what Visual Cafe can generate, or Visual Cafe may be unable to back
  21.         // parse your Java file into its visual environment.
  22.         //{{INIT_CONTROLS
  23.         setLayout(new GridBagLayout());
  24.         setSize(461,241);
  25.         setVisible(false);
  26.         Number1Label.setText("Number 1 =");
  27.         add(Number1Label,new com.symantec.itools.awt.GridBagConstraintsD(0,0,2,2,0.0,0.0,java.awt.GridBagConstraints.CENTER,java.awt.GridBagConstraints.NONE,new Insets(5,27,0,0),0,0));
  28.         Number1Label.setBounds(91,62,79,23);
  29.         add(Number1TextField,new com.symantec.itools.awt.GridBagConstraintsD(2,0,1,1,0.0,0.0,java.awt.GridBagConstraints.CENTER,java.awt.GridBagConstraints.NONE,new Insets(5,52,0,0),89,-2));
  30.         Number1TextField.setBounds(222,62,113,21);
  31.         Number2Label.setText("Number 2 =");
  32.         add(Number2Label,new com.symantec.itools.awt.GridBagConstraintsD(0,2,2,1,0.0,0.0,java.awt.GridBagConstraints.CENTER,java.awt.GridBagConstraints.NONE,new Insets(10,27,0,0),0,0));
  33.         Number2Label.setBounds(91,95,79,23);
  34.         add(Number2TextField,new com.symantec.itools.awt.GridBagConstraintsD(2,2,1,2,0.0,0.0,java.awt.GridBagConstraints.CENTER,java.awt.GridBagConstraints.NONE,new Insets(12,52,0,0),89,0));
  35.         Number2TextField.setBounds(222,97,113,23);
  36.         AnswerLabel.setText("Answer =");
  37.         add(AnswerLabel,new com.symantec.itools.awt.GridBagConstraintsD(0,4,1,1,0.0,0.0,java.awt.GridBagConstraints.CENTER,java.awt.GridBagConstraints.NONE,new Insets(14,34,0,0),0,0));
  38.         AnswerLabel.setBounds(98,134,65,23);
  39.         AnswerTextField.setEditable(false);
  40.         add(AnswerTextField,new com.symantec.itools.awt.GridBagConstraintsD(2,4,1,1,0.0,0.0,java.awt.GridBagConstraints.CENTER,java.awt.GridBagConstraints.NONE,new Insets(14,52,0,0),89,0));
  41.         AnswerTextField.setBackground(java.awt.Color.lightGray);
  42.         AnswerTextField.setBounds(222,134,113,23);
  43.         panel1.setLayout(new GridLayout(4,1,0,2));
  44.         add(panel1,new com.symantec.itools.awt.GridBagConstraintsD(3,0,1,6,0.0,0.0,java.awt.GridBagConstraints.CENTER,java.awt.GridBagConstraints.NONE,new Insets(6,13,23,28),0,0));
  45.         panel1.setBounds(348,63,21,98);
  46.         PlusButton.setActionCommand("button");
  47.         PlusButton.setLabel("+");
  48.         panel1.add(PlusButton);
  49.         PlusButton.setBounds(0,0,21,23);
  50.         SubtractButton.setActionCommand("button");
  51.         SubtractButton.setLabel("-");
  52.         panel1.add(SubtractButton);
  53.         SubtractButton.setBounds(0,25,21,23);
  54.         MultiplyButton.setActionCommand("button");
  55.         MultiplyButton.setLabel("X");
  56.         panel1.add(MultiplyButton);
  57.         MultiplyButton.setBounds(0,50,21,23);
  58.         DivideButton.setActionCommand("button");
  59.         DivideButton.setLabel("/");
  60.         panel1.add(DivideButton);
  61.         DivideButton.setBounds(0,75,21,23);
  62.         setTitle("Simple Calculation");
  63.         //}}
  64.  
  65.         //{{INIT_MENUS
  66.         //}}
  67.  
  68.         //{{REGISTER_LISTENERS
  69.         Window lWindow = new Window();
  70.         addWindowListener(lWindow);
  71.         SymAction lSymAction = new SymAction();
  72.         PlusButton.addActionListener(lSymAction);
  73.         SubtractButton.addActionListener(lSymAction);
  74.         MultiplyButton.addActionListener(lSymAction);
  75.         DivideButton.addActionListener(lSymAction);
  76.         //}}
  77.         
  78.     }
  79.  
  80.     public CalculateFrame(String title)
  81.     {
  82.         this();
  83.         setTitle(title);
  84.     }
  85.  
  86. //    public void show()
  87.     //{
  88. //        move(50, 50);
  89. //        super.show();
  90. //    }
  91.  
  92.  
  93.  
  94.     boolean fComponentsAdjusted = false;
  95.     
  96.     //{{DECLARE_CONTROLS
  97.     java.awt.Label Number1Label = new java.awt.Label();
  98.     java.awt.TextField Number1TextField = new java.awt.TextField(15);
  99.     java.awt.Label Number2Label = new java.awt.Label();
  100.     java.awt.TextField Number2TextField = new java.awt.TextField(15);
  101.     java.awt.Label AnswerLabel = new java.awt.Label();
  102.     java.awt.TextField AnswerTextField = new java.awt.TextField(15);
  103.     java.awt.Panel panel1 = new java.awt.Panel();
  104.     java.awt.Button PlusButton = new java.awt.Button();
  105.     java.awt.Button SubtractButton = new java.awt.Button();
  106.     java.awt.Button MultiplyButton = new java.awt.Button();
  107.     java.awt.Button DivideButton = new java.awt.Button();
  108.     //}}
  109.  
  110.     //{{DECLARE_MENUS
  111.     //}}
  112.  
  113.  
  114.  
  115.     class Window extends java.awt.event.WindowAdapter
  116.     {
  117.         public void windowClosing(java.awt.event.WindowEvent event)
  118.         {
  119.             Object object = event.getSource();
  120.             if (object == CalculateFrame.this)
  121.                 Frame1_WindowClosing(event);
  122.         }
  123.     }
  124.  
  125.  
  126.  
  127.     
  128.  
  129.     class SymAction implements java.awt.event.ActionListener
  130.     {
  131.         public void actionPerformed(java.awt.event.ActionEvent event)
  132.         {
  133.             Object object = event.getSource();
  134.             if (object == PlusButton)
  135.                 plusButton_ActionPerformed(event);
  136.             else if (object == SubtractButton)
  137.                 subtractButton_ActionPerformed(event);
  138.             else if (object == MultiplyButton)
  139.                 multiplyButton_ActionPerformed(event);
  140.             else if (object == DivideButton)
  141.                 divideButton_ActionPerformed(event);
  142.         }
  143.     }
  144.  
  145.     void plusButton_ActionPerformed(java.awt.event.ActionEvent event)
  146.     {
  147.         // to do: code goes here.
  148.              
  149.         plusButton_ActionPerformed_Interaction1(event);
  150.     }
  151.  
  152.  
  153.     void plusButton_ActionPerformed_Interaction1(java.awt.event.ActionEvent event)
  154.     {
  155.     double sum, num1, num2;
  156.  
  157.         try {
  158.             num1 = (Double.valueOf(Number1TextField.getText())).doubleValue();
  159.             num2 = (Double.valueOf(Number2TextField.getText())).doubleValue();
  160.         } catch (NumberFormatException e) { return; }
  161.  
  162.         sum = num1 + num2;
  163.  
  164.         if (sum < 0) { AnswerTextField.setBackground(Color.red);  }
  165.         else         { AnswerTextField.setBackground(Color.lightGray); }
  166.  
  167.         AnswerTextField.setText(String.valueOf(sum));    }
  168.  
  169.  
  170.     void subtractButton_ActionPerformed(java.awt.event.ActionEvent event)
  171.     {
  172.         // to do: code goes here.
  173.         subtractButton_ActionPerformed_Interaction1(event);
  174.     }
  175.         
  176.  
  177.     void subtractButton_ActionPerformed_Interaction1(java.awt.event.ActionEvent event)
  178.     {
  179.         double diff, num1, num2;
  180.  
  181.         try {
  182.             num1 = (Double.valueOf(Number1TextField.getText())).doubleValue();
  183.             num2 = (Double.valueOf(Number2TextField.getText())).doubleValue();
  184.         } catch (NumberFormatException e) { return; }
  185.  
  186.         diff = num1 - num2;
  187.  
  188.         if (diff < 0) { AnswerTextField.setBackground(Color.red);  }
  189.         else          { AnswerTextField.setBackground(Color.gray); }
  190.  
  191.         AnswerTextField.setText(String.valueOf(diff));    
  192.     }
  193.  
  194.     
  195.  
  196.  
  197.     void multiplyButton_ActionPerformed(java.awt.event.ActionEvent event)
  198.     {
  199.         // to do: code goes here.
  200.            multiplyButton_ActionPerformed_Interaction1(event);
  201.     }
  202.  
  203.  
  204.     void multiplyButton_ActionPerformed_Interaction1(java.awt.event.ActionEvent event)
  205.     {
  206.         double product, num1, num2;
  207.  
  208.         try {
  209.             num1 = (Double.valueOf(Number1TextField.getText())).doubleValue();
  210.             num2 = (Double.valueOf(Number2TextField.getText())).doubleValue();
  211.         } catch (NumberFormatException e) { return; }
  212.  
  213.         product = num1 * num2;
  214.  
  215.         if (product < 0) { AnswerTextField.setBackground(Color.red);  }
  216.         else             { AnswerTextField.setBackground(Color.gray); }
  217.  
  218.         AnswerTextField.setText(String.valueOf(product));
  219.     }
  220.  
  221.  
  222.     void divideButton_ActionPerformed(java.awt.event.ActionEvent event)
  223.     {
  224.         // to do: code goes here.
  225.              
  226.         divideButton_ActionPerformed_Interaction1(event);
  227.     }
  228.  
  229.  
  230.     void divideButton_ActionPerformed_Interaction1(java.awt.event.ActionEvent event)
  231.     {
  232.         double quotient, num1, num2;
  233.  
  234.         try {
  235.             num1 = (Double.valueOf(Number1TextField.getText())).doubleValue();
  236.             num2 = (Double.valueOf(Number2TextField.getText())).doubleValue();
  237.         } catch (NumberFormatException e) { return; }
  238.  
  239.         if (num2 == 0) return;
  240.  
  241.         quotient = num1 / num2;
  242.  
  243.         if (quotient < 0) { AnswerTextField.setBackground(Color.red);  }
  244.         else              { AnswerTextField.setBackground(Color.gray); }
  245.  
  246.         AnswerTextField.setText(String.valueOf(quotient));    
  247.     }
  248.  
  249. }
  250.